package com.jinlejia.feedback.idea.ui.adapter;

import androidx.databinding.DataBindingUtil;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.feedback.R;
import com.jinlejia.feedback.databinding.FbItemRespondToDetailsLayoutBinding;
import com.jinlejia.feedback.idea.ui.bean.RespondToDetailsBean;
import com.jinlejia.feedback.question.http.QuestionDetailsHelper;

import java.util.ArrayList;

/**
 * @author SongJiaTai
 * @description:回复详情Adapter
 * @date :2019/7/2 22:43
 */
public class RespondToDetailsAdapter extends YfListAdapter<RespondToDetailsBean>{

    public RespondToDetailsAdapter(ArrayList<RespondToDetailsBean> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_item_respond_to_details_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_empty_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }



    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder){
            bindCurrViewHolder((ViewHolder) holder,position);
        }
    }

    private void bindCurrViewHolder(ViewHolder holder, final int position) {
        final FbItemRespondToDetailsLayoutBinding binding = DataBindingUtil.getBinding(holder.itemView);
        final RespondToDetailsBean respondToDetailsBean = mData.get(position);

        if (mData.size()==position+1) {//如果是最后一个
            binding.lineView.setVisibility(View.GONE);
        }else{
            binding.lineView.setVisibility(View.VISIBLE);
        }

        //用户头像
        Glide.with(binding.userIconImageView.getContext())
                .load(respondToDetailsBean.getCommentUserPortrait())
                .placeholder(R.mipmap.jlj_role_head)
                .error(R.mipmap.jlj_role_head)
                .into(binding.userIconImageView);

        //用户名称
        binding.userNameTextView.setText(respondToDetailsBean.getCommentUserName());


        //用户部门 or  职务
        String userDuty = respondToDetailsBean.getCommentUserDuty();
        String userInfo = String.valueOf(userDuty); //用户职务
        String userDeptStr = respondToDetailsBean.getCommentUserDept(); //用户部门
        if (!TextUtils.isEmpty(userDeptStr)){
            //如果部门为null 则显示职务
            userInfo = userDeptStr;
        }
        binding.userInfoTextView.setText(userInfo);
        //回复时间
        binding.replyTimeTextView.setText(respondToDetailsBean.getGmtCreate());
        //回复内容

        String name = respondToDetailsBean.getCommentTargetUserName();
        Spanned html = Html.fromHtml("回复&nbsp;<font color='#9AA9CD'>"+name+"</font>:&nbsp;"+respondToDetailsBean.getCommentContent());
        binding.replyInfoTextView.setText(html);

        int likeCount;
        boolean isLike;
        try{
            likeCount = respondToDetailsBean.getLikeCount();//点赞数量
        }catch (Exception e){
            likeCount = 0;
        }
        int likeState = respondToDetailsBean.getLikeState();
        if (likeState==0){
            isLike=false;
        }else{
            isLike=true;
        }

        binding.likeCountTextView.setText(String.valueOf(likeCount));
        if (isLike){
            binding.likeStateImageView.setImageResource(R.mipmap.fb_like_icon);
            binding.likeCountTextView.setTextColor(Color.parseColor("#FCCB00"));
        }else {
            binding.likeStateImageView.setImageResource(R.mipmap.fb_unlike_icon);
            binding.likeCountTextView.setTextColor(Color.parseColor("#999999"));
        }

        binding.likeOperateLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = Integer.parseInt(binding.likeCountTextView.getText().toString());

                int likeState = respondToDetailsBean.getLikeState();
                int tempState;
                boolean isLike;
                if (likeState==1){
                    isLike=true;
                }else{
                    isLike =false;
                }
                if (isLike){
                    binding.likeStateImageView.setImageResource(R.mipmap.fb_unlike_icon);
                    binding.likeCountTextView.setTextColor(Color.parseColor("#999999"));
                    tempState = 0;
                    count = count - 1;
                }else {
                    binding.likeStateImageView.setImageResource(R.mipmap.fb_like_icon);
                    binding.likeCountTextView.setTextColor(Color.parseColor("#FCCB00"));
                    count = count + 1;
                    tempState = 1;
                }
                binding.likeCountTextView.setText(String.valueOf(count));

                respondToDetailsBean.setLikeState(tempState);
                respondToDetailsBean.setLikeCount(count);
                if (liketEvent!=null){
                    liketEvent.onLikeListener(isLike,respondToDetailsBean.getCommentCode(),QuestionDetailsHelper.LikeType.Comment,respondToDetailsBean,position);
                }
            }
        });

        binding.replyInfoTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replyInfoEvent.onReplyInfoListener(respondToDetailsBean.getReplyCode(),respondToDetailsBean.getCommentUserId(),respondToDetailsBean.getCommentUserName());
            }
        });
    }


    private replyInfoEvent replyInfoEvent;

    public void setReplyInfoEvent(RespondToDetailsAdapter.replyInfoEvent replyInfoEvent) {
        this.replyInfoEvent = replyInfoEvent;
    }

    public interface replyInfoEvent{
        void onReplyInfoListener(String replyCode,String commentTargetUserId,String commentTargetUserName);
    }






    private LiketEvent liketEvent;

    public interface LiketEvent{
        void onLikeListener(boolean islike,String likeCode, QuestionDetailsHelper.LikeType likeType, RespondToDetailsBean respondToDetailsBean, int position);
    }

    public void setLiketEvent(LiketEvent liketEvent) {
        this.liketEvent = liketEvent;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
}
